package com.platform.controller;

import com.platform.entity.CartEntity;
import com.platform.entity.CategoryEntity;
import com.platform.entity.GoodsEntity;
import com.platform.entity.ProductEntity;
import com.platform.service.CartService;
import com.platform.service.CategoryService;
import com.platform.service.GoodsService;
import com.platform.service.ProductService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-21 21:19:49
 */
@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ProductService productService;

    @Autowired
    private CartService cartService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("goods:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        query.put("isDelete", 0);
        List<GoodsEntity> goodsList = goodsService.queryList(query);

        if (goodsList.size() > 0) {
            for (GoodsEntity goodsEntity : goodsList) {
                goodsEntity.setWxAppLink("../goodDetail/goodDetail?goodId=" + goodsEntity.getId());
            }
        }

        int total = goodsService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(goodsList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("goods:info")
    public R info(@PathVariable("id") Integer id) {
        GoodsEntity goods = goodsService.queryObject(id);

        return R.ok().put("goods", goods);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("goods:save")
    public R save(@RequestBody GoodsEntity goods) {

        Integer categoryId = goods.getCategoryId();
        if (categoryId == null) {
            return R.error("请选择商品分类");
        }
        CategoryEntity categoryEntity = categoryService.queryObject(categoryId);
        if (categoryEntity.getLevel().equals("L1")) {
            return R.error("请选择二级分类");
        }

        goods.setSellVolume(0);


        Integer isFullReduction = goods.getIsFullReduction();
        Integer isSecondHalfPrice = goods.getIsSecondHalfPrice();
        Integer isRebate = goods.getIsRebate();

        Boolean flag = isFullReduction == 0 && isSecondHalfPrice == 0 && isRebate == 0;
        Boolean flag1 = isFullReduction == 1 && isSecondHalfPrice == 0 && isRebate == 0;
        Boolean flag2 = isFullReduction == 0 && isSecondHalfPrice == 1 && isRebate == 0;
        Boolean flag3 = isFullReduction == 0 && isSecondHalfPrice == 0 && isRebate == 1;

        if (!(flag || flag1 || flag2 || (flag3))) {
            return R.error("只能选择一种优惠或不选");
        }


        goodsService.save(goods);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("goods:update")
    public R update(@RequestBody GoodsEntity goods) {
        Integer isFullReduction = goods.getIsFullReduction();
        Integer isSecondHalfPrice = goods.getIsSecondHalfPrice();
        Integer isRebate = goods.getIsRebate();

        Boolean flag = isFullReduction == 0 && isSecondHalfPrice == 0 && isRebate == 0;
        Boolean flag1 = isFullReduction == 1 && isSecondHalfPrice == 0 && isRebate == 0;
        Boolean flag2 = isFullReduction == 0 && isSecondHalfPrice == 1 && isRebate == 0;
        Boolean flag3 = isFullReduction == 0 && isSecondHalfPrice == 0 && isRebate == 1;

        if (!(flag || flag1 || flag2 || (flag3))) {
            return R.error("只能选择一种优惠或不选");
        }


        goodsService.update(goods);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("goods:delete")
    public R delete(@RequestBody Integer[] ids) {
        goodsService.deleteBatch(ids);

        for (Integer id : ids) {
            //先查询该商品旗下的产品列表
            Map<String, Object> params = new HashMap<>();
            params.put("goodsId", id);

            List<ProductEntity> productEntities = productService.queryList(params);
            if (productEntities.size() > 0) {
                for (ProductEntity productEntity : productEntities) {
                    //查询购物车表，如果有数据则设置为不可用
                    Map<String, Object> param = new HashMap<>();
                    param.put("productId", productEntity.getId());
                    List<CartEntity> cartEntities = cartService.queryListByCondition(param);
                    if (cartEntities.size() > 0) {
                        for (CartEntity cartEntity : cartEntities) {
                            cartEntity.setProductStatus(0);//设置为下架状态
                            cartService.update(cartEntity);
                        }
                    }
                }
            }
        }

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        params.put("isDelete", 0);
        List<GoodsEntity> list = goodsService.queryList(params);

        return R.ok().put("list", list);
    }


    /**
     * 商品回收站
     *
     * @param params
     * @return
     */
    @RequestMapping("/historyList")
    public R historyList(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        query.put("isDelete", 1);
        List<GoodsEntity> goodsList = goodsService.queryList(query);
        int total = goodsService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(goodsList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 商品从回收站恢复
     */
    @RequestMapping("/back")
    @RequiresPermissions("goods:back")
    public R back(@RequestBody Integer[] ids) {
        goodsService.back(ids);

        for (Integer id : ids) {
            //先查询该商品旗下的产品列表
            Map<String, Object> params = new HashMap<>();
            params.put("goodsId", id);

            List<ProductEntity> productEntities = productService.queryList(params);
            if (productEntities.size() > 0) {
                for (ProductEntity productEntity : productEntities) {
                    //查询购物车表，如果有数据则设置为不可用
                    Map<String, Object> param = new HashMap<>();
                    param.put("productId", productEntity.getId());
                    List<CartEntity> cartEntities = cartService.queryListByCondition(param);
                    if (cartEntities.size() > 0) {
                        for (CartEntity cartEntity : cartEntities) {
                            cartEntity.setProductStatus(1);//设置为正常状态
                            cartService.update(cartEntity);
                        }
                    }
                }
            }
        }
        return R.ok();
    }

    /**
     * 总计
     */
    @RequestMapping("/queryTotal")
    public R queryTotal(@RequestParam Map<String, Object> params) {

        params.put("isDelete", 0);
        int sum = goodsService.queryTotal(params);
        return R.ok().put("goodsSum", sum);
    }

    /**
     * 上架
     */
    @RequestMapping("/enSale")
    public R enSale(@RequestBody Integer id) {

        GoodsEntity goodsEntity = goodsService.queryObject(id);
        if (goodsEntity != null && goodsEntity.getIsOnSale() == 1) {
            return R.error("该商品已经是上架状态");
        }


        goodsService.enSale(id);


        //查看是否有上架的产品
        Map<String, Object> params = new HashMap<>();
        params.put("goodsId", id);
        List<ProductEntity> productEntityList = productService.queryList(params);
        if (productEntityList.size() == 0) {
            return R.error("清先添加上架的产品");
        }

        for (ProductEntity productEntity : productEntityList) {
            //商品上架，全部产品都上架
            productEntity.setIsOnSale(1);
            productService.update(productEntity);

            //查询购物车表，如果有数据则设置为可用
            Map<String, Object> param = new HashMap<>();
            param.put("productId", productEntity.getId());
            List<CartEntity> cartEntities = cartService.queryListByCondition(param);
            if (cartEntities.size() > 0) {
                for (CartEntity cartEntity : cartEntities) {
                    cartEntity.setProductStatus(1);//设置为上架状态
                    cartService.update(cartEntity);
                }
            }
        }


        return R.ok();
    }

    /**
     * 下架
     */
    @RequestMapping("/unSale")
    public R unSale(@RequestBody Integer id) {
        GoodsEntity goodsEntity = goodsService.queryObject(id);
        if (goodsEntity != null && goodsEntity.getIsOnSale() == 0) {
            return R.error("该商品已经是下架状态");
        }
        goodsService.unSale(id);


        //先查询该商品旗下的产品列表
        Map<String, Object> params = new HashMap<>();
        params.put("goodsId", id);

        List<ProductEntity> productEntities = productService.queryList(params);
        if (productEntities.size() > 0) {
            for (ProductEntity productEntity : productEntities) {
                //商品下架，全部产品都下架
                productEntity.setIsOnSale(0);
                productService.update(productEntity);
                //查询购物车表，如果有数据则设置为不可用
                Map<String, Object> param = new HashMap<>();
                param.put("productId", productEntity.getId());
                List<CartEntity> cartEntities = cartService.queryListByCondition(param);
                if (cartEntities.size() > 0) {
                    for (CartEntity cartEntity : cartEntities) {
                        cartEntity.setProductStatus(0);//设置为下架状态
                        cartService.update(cartEntity);
                    }
                }
            }
        }


        return R.ok();
    }
}
